'use strict';

var 
    form = $('#test-form'),
    langs = form.find('[name=lang]'),
    selectAll = form.find('label.selectAll :checkbox'),
    selectAllLabel =form.find('span.selectAll'),
    deselectAllLabel = form.find('span.deselectAll'),
    invertSelect =form.find('a.invertSelect');

    console.log(langs);
    console.log(langs);
    console.log(selectAll);
    console.log(selectAllLabel);
    console.log(deselectAllLabel);
    console.log(invertSelect);

    // // 重置初始化状态:
    // form.find('*').show().off();
    // form.find(':checkbox').prop('checked', false).off();
    // deselectAllLabel.hide();
    // // 拦截form提交事件:
    // form.off().submit(function (e) {
    // e000000000000.preventDefault();
    // alert(form.serialize());
    // });

    langs.click(function(){
        if(langs.not('input:checked').length===0){
            selectAll.prop('checked',true);

            selectAllLabel.css('display','none');

            deselectAllLabel.css('display','inline');
        }
        else{
            selectAll.prop('checked',false);

            selectAllLabel.css('display','inline');

            deselectAllLabel.css('display','none');
        }
    });

    selectAll.click(function(){
        if(selectAll.is(':checked')){
            langs.prop('checked',true);
            
            selectAllLabel.css('display','none');

            deselectAllLabel.css('display','inline');
        }
        else{
            langs.prop('checked',false);

            selectAllLabel.css('display','inline');

            deselectAllLabel.css('display','none')
        }
    });

    invertSelect.click(function(){
        langs.map(function(){
            var ch = $(this);
            if(ch.prop('checked')){
                ch.prop('checked',false);
            }
            else{
                ch.prop('checked',true);
            }
        });
        if(langs.not(':checked').length===langs.length){
            selectAll.prop('checked',false);
        }
        else if(langs.not(':checked').length===0){
            selectAll.prop('checked',true);
        }
    });